package desktop.playerguide.splashmodel;

import static pulpcore.image.Colors.WHITE;
import static pulpcore.math.CoreMath.rand;
import desktop.playerguide.PlayerGuideScene;
import pulpcore.Input;
import pulpcore.Stage;
import pulpcore.animation.Easing;
import pulpcore.animation.Timeline;
import pulpcore.animation.event.RemoveSpriteEvent;
import pulpcore.image.BlendMode;
import pulpcore.image.CoreFont;
import pulpcore.image.CoreImage;
import pulpcore.math.CoreMath;
import pulpcore.scene.Scene2D;
import pulpcore.sprite.Group;
import pulpcore.sprite.ImageSprite;
import pulpcore.sprite.Label;
import pulpcore.sprite.Sprite;

public class Splash extends Scene2D {
	    
		ImageSprite background;
	    CoreImage[] images;
	    Group particleLayer;
	    double lastX = Stage.getWidth();
	    double lastY = 0.0;
	    boolean wasMouseInside;
    	double x = 0.0;
    	double y = Stage.getHeight();
    	CoreImage[] letterImageArray = new CoreImage[8];//jcolumns six letters
    	 Label clickMessage;
    	
	    @Override
	    public void load() {
	        background = new ImageSprite(CoreImage.load("splash.jpg"),
	        		0,0,Stage.getWidth(),Stage.getHeight());
	        images = CoreImage.load("particles.png").split(6, 1);
	        
	        for (int i = 0; i < letterImageArray.length; i++) {
				letterImageArray[i] = CoreImage.load("letter" + i + ".png");
			}
	        
	        particleLayer = new Group();
	        // Particles should ignore mouse input
	        particleLayer.enabled.set(false);
	        // Particles look good with additive blending
	        particleLayer.setBlendMode(BlendMode.Add());
	        add(background);
	        addLayer(particleLayer);
	        
	        clickMessage = new Label(CoreFont.getSystemFont().tint(WHITE),
		            "press enter to start !", Stage.getWidth()*0.40, Stage.getHeight()/2);
	        
	        makeParticles(320, 240, 320, 240, 50); 
	        setCursor(Input.CURSOR_OFF);
	          	   
 	        int dist = (int)Math.sqrt((lastX - x) * (lastX - x) + (lastY - y) * (lastY - y)); 
 	    	makeParticles(lastX, lastY, x, y, 2 + dist / 8); 	   	
 	    	
 	    	position();
 	    	add(clickMessage);
	    }
	    
	    private void position(){
	    	for (int i = 0; i < Stage.getWidth()/90; i++) {
				LetterImage letterImage = new LetterImage(
						letterImageArray[i%(letterImageArray.length)],
						i*90,rand(Stage.getHeight()));
				add(letterImage);
			}
	    };
	    
	    @Override
	    public void update(int elapsedTime) {
	    	super.update(elapsedTime);
	    	
	    	if(Input.isDown(Input.KEY_ENTER)){
	    		Stage.popScene();
	    		Stage.pushScene(new PlayerGuideScene());
	    	};
	    	randFireworks();
	    }
	    
	    private void randFireworks(){
	    	double randX = rand(0,Stage.getWidth());
	    	double randY = rand(0,Stage.getHeight());
	    	 makeParticles( randX, randY, randX + 10, randY + 10, 50);
	    }
	    
	    private void makeParticles(double x1, double y1, double x2, double y2, double numParticles) {
	    	
	        Timeline timeline = new Timeline();
	        
	        for (int i = 0; i < numParticles; i++) {
	            int size = CoreMath.rand(4, 48);
	            int duration = (200 - size) * 3;
	            int moveDistance = CoreMath.rand(4, 80 - size);
	            double moveDirection = CoreMath.rand(0, 2*Math.PI);
	            
	            double startX = x1 + i * (x2 - x1) / numParticles;
	            double startY = y1 + i * (y2 - y1) / numParticles;
	            double goalX = startX + (moveDistance * Math.cos(moveDirection));
	            double goalY = startY + (moveDistance * Math.sin(moveDirection));
	            double startAngle = CoreMath.rand(0, 2*Math.PI);
	            double endAngle = startAngle + CoreMath.rand(-2*Math.PI, 2*Math.PI);
	            
	            CoreImage image = images[CoreMath.rand(images.length - 1)];
	            Sprite sprite = new ImageSprite(image, startX, startY);
	            sprite.setAnchor(0.5, 0.5);
	            sprite.setSize(size, size);
	            particleLayer.add(sprite);
	            
	            timeline.animateTo(sprite.x, goalX, duration, Easing.REGULAR_OUT);
	            timeline.animateTo(sprite.y, goalY, duration, Easing.REGULAR_OUT);
	            timeline.animate(sprite.angle, startAngle, endAngle, duration);
	            timeline.at(100).animateTo(sprite.alpha, 0, duration - 100, Easing.REGULAR_OUT);
	            timeline.add(new RemoveSpriteEvent(particleLayer, sprite, duration));
	        }
	        
	        addTimeline(timeline);
	    }
}
